﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Controllers
{
    [ValidateInput(false)]
    public class UserInfoController : Controller
    {
        //
        // GET: /UserInfo/
        [HttpGet]
        public ActionResult Index()
        {
            var resp = Framework.Data.RepositoryFactory.GetRepository();
            var userinfo = resp.GetByKey<Dal.Entities.User>(Common.GetUserID());
            if (userinfo == null)
            {
                userinfo = new Dal.Entities.User { ID = Common.GetUserID() };
                resp.AddThenSaveChange(userinfo);
            }
            #region 地域处理
            var plist = resp.Get<Dal.Entities.Region>(i => i.depth == 2).Select(i => new SelectListItem { Value = i.Id.ToString(), Text = i.BriefName }).ToList();
            plist.Insert(0, new SelectListItem { Text = "请选择", Value = "-1" });
            if (userinfo.ProvinceID.HasValue)
            {
                foreach (var item in plist)
                {
                    if (item.Value == userinfo.ProvinceID.Value.ToString())
                    {
                        item.Selected = true;
                        break;
                    }
                }
            }
            ViewData["ProvinceID"] = plist;

            var clist = resp.Get<Dal.Entities.Region>(i => i.depth == 3 && i.provinceid == userinfo.ProvinceID).Select(i => new SelectListItem { Value = i.Id.ToString(), Text = i.BriefName }).ToList();
            clist.Insert(0, new SelectListItem { Text = "请选择", Value = "-1" });
            if (userinfo.CityID.HasValue)
            {
                foreach (var item in clist)
                {
                    if (item.Value == userinfo.CityID.Value.ToString())
                    {
                        item.Selected = true;
                        break;
                    }
                }
                ViewData["CityID"] = clist;
            }
            else
            {
                ViewData["CityID"] = new List<SelectListItem> { new SelectListItem { Text = "请选择", Value = "-1" } };
            }
            #endregion
            if (
               !string.IsNullOrWhiteSpace(userinfo.DoctorMobile) &&
                !string.IsNullOrWhiteSpace(userinfo.DoctorName) &&
                !string.IsNullOrWhiteSpace(userinfo.HospitalName) &&
                !string.IsNullOrWhiteSpace(userinfo.TitleName) &&
                !string.IsNullOrWhiteSpace(userinfo.SubjectName))
            {
                ViewBag.CannotModify = true;
            }

            return View(userinfo);
        }

        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            var resp = Framework.Data.RepositoryFactory.GetRepository();
            var userinfo = resp.GetByKey<Dal.Entities.User>(Common.GetUserID());
            resp.Attach(userinfo);
            TryUpdateModel<Dal.Entities.User>(userinfo);
            ViewBag.Result = resp.Save() > 0;
            Common.AlertRedirect("操作成功", "/userinfo");
            return new EmptyResult();
        }

    }
}
